#ifndef _THREND_HPP_
#define _THREND_HPP_
#include <iostream>
#include <string>
#include <pthread.h>
#include <functional>
#include <sys/types.h>
#include <unistd.h>
#include<mysql/mysql.h>

const std::string global_host = "127.0.0.1";
const std::string global_user = "zj";
const std::string global_passwd = "050716";
const std::string global_db = "ConnectTest";
const unsigned int global_port = 8080;

enum
{
    FAILED_BEGIN = 1,
    FAILED_SQL = 2,
    FAILED_COMMIT = 3
};

namespace ThreadModule
{
 
    static int number = 1;
    enum class TSTATUS
    {
        NEW,
        RUNNING,
        STOP
    };

    class Thread
    {
    private:
        using func_t = std::function<void(std::string name,MYSQL* mysql)>;//线程执行的对象的类型
        // 成员方法！
        static void *Routine(void *args)
        {
            Thread *t = static_cast<Thread *>(args);
            t->_status = TSTATUS::RUNNING;
            t->_func(t->Name(),t->Return_MYSQL());//去执行func
            return nullptr;
        }
        void EnableDetach() { _joinable = false; }

    public:
        Thread(func_t func) : _func(func), _status(TSTATUS::NEW), _joinable(true)
        {
            _name = "Thread-" + std::to_string(number++);
            _pid = getpid();

            //每个线程都和数据库进行连接
            _mysql = mysql_init(nullptr); // 连接数据库
            mysql_real_connect( _mysql, global_host.c_str(), global_user.c_str(), global_passwd.c_str(), global_db.c_str(), global_port, nullptr, 0);
            mysql_set_character_set(_mysql, "utf8");

        }
        Thread() : _status(TSTATUS::NEW), _joinable(true)
        {
            _name = "Thread-" + std::to_string(number++);
            _pid = getpid();
        }
        bool Start()
        {
            if (_status != TSTATUS::RUNNING)
            {
                int n = ::pthread_create(&_tid, nullptr, Routine, this); // TODO
                if (n != 0)
                    return false;
                return true;
            }
            return false;
        }
        bool Stop()
        {
            if (_status == TSTATUS::RUNNING)
            {
                int n = ::pthread_cancel(_tid);
                std::cout << "我调用了pthread_cancel" << std::endl;
                if (n != 0)
                    return false;
                _status = TSTATUS::STOP;
                return true;
            }
            return false;
        }

        bool Join()
        {
            if (_joinable)
            {
                void *ret = nullptr;
                int n = ::pthread_join(_tid, &ret);
                std::cout << "我调用了pthread_join" << std::endl;

                if (n != 0)
                    return false;
                _status = TSTATUS::STOP;
                return true;
            }
            return false;
        }

        void Detach()
        {
            EnableDetach();
            pthread_detach(_tid);
        }
        bool IsJoinable() { return _joinable; }
        std::string Name() { return _name; }

        MYSQL* Return_MYSQL()
        {
            return _mysql;
        }

        ~Thread()
        {
        }

    private:
        std::string _name;
        pthread_t _tid;
        pid_t _pid;
        bool _joinable; // 是否是分离的，默认不是
        func_t _func;
        TSTATUS _status;
        MYSQL *_mysql;
    };
}
/*namespace ThreadModule
{
    enum class TSTATUS
    {
        NEW,
        RUNNING,
        STOP
    };

    static int number = 1;
    template <typename T>
    class Thread
    {
    public:
        using func_t = std::function<void(T)>;

    private:
        // 成员方法！
        static void *Routine(void *args)
        {
            Thread *t = static_cast<Thread<T> *>(args);
            t->_status = TSTATUS::RUNNING;
            t->_func(t->_data);
            return nullptr;
        }
        void EnableDetach() { _joinable = false; }

    public:
        Thread(func_t func, T data) : _func(func), _status(TSTATUS::NEW), _joinable(true), _data(data)
        {
            _name = "Thread-" + std::to_string(number++);
            _pid = getpid();
        }
        Thread() : _status(TSTATUS::NEW), _joinable(true)
        {
            _name = "Thread-" + std::to_string(number++);
            _pid = getpid();
        }
        bool Start()
        {
            if (_status != TSTATUS::RUNNING)
            {
                int n = ::pthread_create(&_tid, nullptr, Routine, this); // TODO
                if (n != 0)
                    return false;
                return true;
            }
            return false;
        }
        bool Stop()
        {
            if (_status == TSTATUS::RUNNING)
            {
                int n = ::pthread_cancel(_tid);
                std::cout << "我调用了pthread_cancel" << std::endl;
                if (n != 0)
                    return false;
                _status = TSTATUS::STOP;
                return true;
            }
            return false;
        }

        bool Join()
        {
            if (_joinable)
            {
                void *ret = nullptr;
                int n = ::pthread_join(_tid, &ret);
                std::cout << "我调用了pthread_join" << std::endl;

                if (n != 0)
                    return false;
                _status = TSTATUS::STOP;
                return true;
            }
            return false;
        }

        void Detach()
        {
            EnableDetach();
            pthread_detach(_tid);
        }
        bool IsJoinable() { return _joinable; }
        std::string Name() { return _name; }
        ~Thread()
        {
        }

    private:
        std::string _name;
        pthread_t _tid;
        pid_t _pid;
        bool _joinable; // 是否是分离的，默认不是
        func_t _func;
        TSTATUS _status;
        T _data;
    };
}*/

#endif

// #endif
